/*
 * @(#)MaintUserPwdAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.user.web;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.Crypt;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.UserRecordDAObject;

/**
  MaintUserPwdAction.java

  This class is for maint user record.

    @author          Scott Tong
    @company         DCIVision Limited
    @creation date   07/08/2003
    @version         $Revision: 1.5 $
*/

public class MaintUserPwdAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.5 $";

    
    /** Creates a new instance of MaintUserPwdAction */
    public MaintUserPwdAction() {
        super();
        //this.setListName("userPwdList");
    }
    
     /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.user.dao.UserRecordDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.STP_USER_PWD);
  }
    public void createRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    }
    
    public void deleteRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    }
    
    public void selectRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
        Connection conn = this.getConnection(request);
        log.debug("selectrecord maintUserPwd");
        String ID = "";
        Integer requestID = null;
        try {
            SessionContainer sessionCtn = this.getSessionContainer(request);
            log.debug("form.getID() is " + form.getID() );
            log.debug("id is "+ request.getAttribute("ID"));
            requestID = (Integer)request.getAttribute("ID");
            ID = form.getID();
            if(ID == null)
              ID = requestID.toString();
            // Get the User Record.
            UserRecordDAObject userRecordDAO = new UserRecordDAObject(sessionCtn, conn);
            UserRecord userRecord = (UserRecord)userRecordDAO.getObjectByID(com.dcivision.framework.TextUtility.parseIntegerObj(ID));
            form.setFormData(userRecord);
        } catch (ApplicationException appEx) {
            throw appEx;
        } catch (Exception e){
            log.error(e, e);
            throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR);
        } 
        /*
        finally {
            try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
        }
        */
    }
    
    public void updateRecord(ActionMapping actionMapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
        Connection conn = this.getConnection(request);
        try {
            SessionContainer sessionCtn = this.getSessionContainer(request);
            log.debug("updateRecord in maintUserPwd");
            // Get the DB User Record.
            UserRecordDAObject userRecordDAO = new UserRecordDAObject(sessionCtn, conn);
            UserRecord userRecord = (UserRecord)userRecordDAO.getObjectByID(com.dcivision.framework.TextUtility.parseIntegerObj(form.getID()));
            
            String sOrigPwd = request.getParameter("origPwd");
            String sNewPwd = request.getParameter("loginPwd");
           
            String encrptedPwd = Crypt.encrypt(sOrigPwd, SystemParameterFactory.getSystemParameter(SystemParameterConstant.CRYPTO_SALT));
            
            if (!userRecord.getLoginPwd().equals(encrptedPwd)){
                throw new ApplicationException(ErrorConstant.LOGIN_ORIGINAL_PASSWORD_INCORRECT);
            }else{
                userRecord.setLoginPwd(sNewPwd);
                userRecord.setLastPwdUpdateDate(Utility.getCurrentTimestamp());
                userRecord = (UserRecord)userRecordDAO.updateObject(userRecord);
            }
            commit(request);
            
            form.setFormData(userRecord);
            log.debug("before catch");
            //refreshList(request, GlobalConstant.OP_MODE_UPDATE, userRecord);
            //sessionCtn.getApplicationContext().updateUserNme(userRecord.getID());
      
        } catch (ApplicationException appEx) {
            //this.rollback(conn);
            throw appEx;
        } catch (Exception e) {
            //log.error(e, e);
            //this.rollback(conn);
            throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR);
        } 
        /*
        finally {
            try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
        }
        */
    }
    
}
